package com.shujia.uerService;

import com.shujia.Dao.weiboDao;
import com.shujia.JdbcUtil.RedisUtil;
import com.shujia.user.Weibo;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

public class weiboService {
    private weiboDao weiboDao = new weiboDao();
    public Weibo hostSearch(int id){

        Jedis jedis = null;
        String key = String.valueOf(id);
        try{
            jedis = RedisUtil.getConnction();
            Map<String, String> map = jedis.hgetAll(key);
            if(map != null && !map.isEmpty()){
                Weibo weibo = new Weibo();
                weibo.setId(id);
                weibo.setText(map.get("text"));
                jedis.expire(key, 10);
                return weibo;
            }
        }catch  (Exception e) {
            e.printStackTrace();
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
        Weibo weibo = weiboDao.hostSearch(id);
        if(weibo==null){
            return null;
        }
        try {
            jedis = RedisUtil.getConnction();
            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put("text",weibo.getText());
            jedis.hmset(key, hashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return weibo;
    }

}
